package com.kedou.sdk.ui.activity;

import android.Manifest;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.Toast;

import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.handler.SimpleAsyncResponseHandler;
import com.kedou.sdk.params.ResponseParams;
import com.kedou.sdk.params.SendVerifyResponseParams;
import com.kedou.sdk.params.bean.State;
import com.kedou.sdk.ui.assist.OnPhoneVerifyCodeListener;
import com.kedou.sdk.ui.assist.OnRequestVerifyCodeListener;
import com.kedou.sdk.utils.ToolsUtils;

/**
 * Created by Administrator on 2017/6/8 0008.
 */

public abstract class PhoneVerifyActivity extends AppBaseActivity implements OnRequestVerifyCodeListener {

    private int mSendVerifyCodeTaskId;

    private OnPhoneVerifyCodeListener listener;

    @Override
    protected void onStop() {
        super.onStop();
        HttpEngineApi.getInstance().cancel(mSendVerifyCodeTaskId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onCancelCountDownTimer();
    }

    public final void setOnPhoneVerifyCodeListener(OnPhoneVerifyCodeListener listener) {
        this.listener = listener;
    }

    protected final void onStartCountDownTimer() {
        if (listener != null) {
            listener.onStartCountDownTimer();
        }
    }

    protected final void onCancelCountDownTimer() {
        if (listener != null) {
            listener.onCancelCountDownTimer();
        }
    }

    protected final void onCallbackVerifyCode(String verifyCode) {
        if (listener != null) {
            listener.onCallbackVerifyCode(verifyCode);
        }
    }

    @Override
    public void doRequestPhoneVerifyCode(String verify, String verifyType) {
        SimpleAsyncResponseHandler handler = requestCallbackHandler(SendVerifyResponseParams.class, verify);
        mSendVerifyCodeTaskId = HttpEngineApi.getInstance().sendPhoneVerifyCode(verify, verifyType, handler);
    }

    @Override
    public void onAsyncRequestStart(int flag, Object... params) {
        super.onAsyncRequestStart(flag, params);
        if (mSendVerifyCodeTaskId == flag) {
            onStartCountDownTimer();
        }
    }

    @Override
    public void onAsyncRequestException(int flag, Exception ex) {
        super.onAsyncRequestException(flag, ex);
        if (mSendVerifyCodeTaskId == flag) {
            onCancelCountDownTimer();
        }
    }

    @Override
    public void onAsyncRequestNetworkException(int flag, Exception ex) {
        super.onAsyncRequestNetworkException(flag, ex);
        if (mSendVerifyCodeTaskId == flag) {
            onCancelCountDownTimer();
        }
    }

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);

        if (mSendVerifyCodeTaskId == flag && response != null) {
            if (State.SUCCESS == response.getCode()) {
                //如果当前使用的手机跟输入的手机号码相同，自动填入手机验证码
                if (response instanceof SendVerifyResponseParams) {

                    String userPhoneNumber = null;
                    if (ToolsUtils.checkAppSelfPermission(getApplicationContext(), Manifest.permission.READ_PHONE_STATE)) {
                        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                        userPhoneNumber = tm.getLine1Number();
                    }

                    SendVerifyResponseParams.SendVerifyResponseData data = ((SendVerifyResponseParams) response).getData();
                    if (data != null) {
                        String currentPhoneNumber = data.getPhone();
                        if (TextUtils.equals(userPhoneNumber, currentPhoneNumber)) {
                            String verifyCode = ((SendVerifyResponseParams) response).getData().getCode();
                            onCallbackVerifyCode(verifyCode);
                        }
                    }
                }

                Toast.makeText(this, response.getMsg(), Toast.LENGTH_SHORT).show();
            } else {
                onCancelCountDownTimer();
            }
        } else {
            if (response != null) {
                if (State.SUCCESS == response.getCode()) {
                    onCancelCountDownTimer();
                }
            }
        }
    }

}
